package kr.co.xosoft.db;

import kr.co.xosoft.common.Config;
import kr.co.xosoft.vo.ServerChkVO;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DBHandler {
	private Context context;
	private DBHelper dbHelper;
	private SQLiteDatabase db;
	
	//autoflagyn bool not null, waittime integer not null, viewtype integer not null
	
	public DBHandler(Context context) {
		this.context = context;
		this.dbHelper = new DBHelper(context);
		this.db = dbHelper.getWritableDatabase();
	}
	
	public static DBHandler open(Context context){
		DBHandler handler = new DBHandler(context);
		return handler;
	}
	
	public void close(){
		dbHelper.close();
	}
	
	public long insert(boolean flag, int waitTime, int viewType){
		long result = 0;
		try{
			ContentValues values = new ContentValues();
			values.put("autoflagyn", flag);
	    	values.put("waittime", waitTime);
	    	values.put("viewtype", viewType);
			result = db.insert(Config.TABLE_NAME, null, values);
		}catch (Exception e) {
			Log.e("N1", e.toString());
		}
		return result;
	}
	
	public long updateWaitTime(String id, int waitTime) {
		long result = 0;
		try{
	    	ContentValues contentValues = new ContentValues();
	    	contentValues.put("waittime", waitTime);
	    	result = db.update(Config.TABLE_NAME, contentValues, "id = ?", new String[]{id});
		}catch (Exception e) {
			Log.e("N1", e.toString());
		}
    	return result;
	}
	
	public long updateAutoFlagYN(String id, boolean flag) {
		long result = 0;
		try{
			ContentValues contentValues = new ContentValues();
			contentValues.put("autoflagyn", flag);
			result = db.update(Config.TABLE_NAME, contentValues, "id = ?", new String[]{id});
		}catch (Exception e) {
			Log.e("N1", e.toString());
		}
		return result;
	}
	
	public long updateViewType(String id, int viewtype) {
		long result = 0;
		try{
			ContentValues contentValues = new ContentValues();
			contentValues.put("viewtype", viewtype);
			result = db.update(Config.TABLE_NAME, contentValues, "id = ?", new String[]{id});
		}catch (Exception e) {
			Log.e("N1", e.toString());
		}
		return result;
	}
	
	public long updateAll(String id, boolean flag, int waitTime, int viewtype) {
		long result = 0;
		try{
			ContentValues contentValues = new ContentValues();
			contentValues.put("autoflagyn", flag);
			contentValues.put("waittime", waitTime);
			contentValues.put("viewtype", viewtype);
			result = db.update(Config.TABLE_NAME, contentValues, "id = ?", new String[]{id});
		}catch (Exception e) {
			Log.e("N1", e.toString());
		}
		return result;
	}
	public int selectCount(){
		Cursor cursor = null;
		int count = 0;
		try{
			cursor = db.query(true, Config.TABLE_NAME, null,null,null,null,null,null,null);
			count = cursor.getCount();
		}catch (Exception e) {
			Log.e("N1", e.toString());
		}finally{
			cursor.close();
		}
		return count;
	}
	
	public ServerChkVO select(){
		ServerChkVO chkVO = null;
		Cursor cursor = null;
		try{
			cursor = db.query(true, Config.TABLE_NAME, null,null,null,null,null,null,null);
			chkVO = new ServerChkVO();
			if(cursor != null && cursor.getCount() > 0){
				cursor.moveToFirst();
				while(!cursor.isAfterLast()){
					chkVO.setIdx(cursor.getInt(0));
					chkVO.setAutoflag(cursor.getString(1).equals("1")?true:false);
					chkVO.setWaittime(cursor.getInt(2));
					chkVO.setViewtype(cursor.getInt(3));
					cursor.moveToNext();
				}
			}
		}catch (Exception e) {
			Log.e("N1", e.toString());
		}finally{
			cursor.close();
		}
		return chkVO;
	}
}
